<?php

/**
 * Publish New Advert
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 8 Apr 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Publish extends LB_Controller {

  public function index() {

    // Get Categories
    $categories = $this->category_model->get_categories();

    // Get Selected Category if Any 
    $category_id = (int) $this->input->get('category', true);
    $category_id = (int) (!empty($category_id)) ? $category_id : 1;
    $category_data = $this->category_model->get_category($category_id);

    // Check Category Exists
    if (!empty($category_data)) {

      // Init vars
      $advert_type = null;
      $advert_item = null;
      $advert_item_type = null;
      $advert_title = null;
      $advert_description = null;
      $advert_price_type = null;
      $advert_price_value = null;
      $advert_price_currency = null;
      $advert_zip_code = null;
      $advert_locality = null;
      $advert_images = array();
      $advert_year_model = null;
      $advert_brand = null;
      $advert_brand_model = null;

      // Getting data
      $items = $this->filter_model->get_items($category_id);
      $item_types = $this->filter_model->get_item_types($category_id);
      $item_extras = $this->filter_model->get_item_extras($category_id);
      $brands = $this->filter_model->get_brands($category_id);
      $brand_models = $this->filter_model->get_brand_models($category_id);
      $give_for = $this->filter_model->get_give_for();
      $peoples = $this->filter_model->get_peoples();

      // On form submit
      if ($this->is_post()) {

        // Validate form
        $this->form_validation->set_rules('advert_title', 'Advert title', 'required|min_length[50]|max_length[250]');

        // Form success
        if ($this->form_validation->run() == true) {

          echo 'form valid';
        }


        // Getting submit values
        $advert_type = $this->input->post('advert_type', true);
        $advert_item = $this->input->post('advert_item', true);
        $advert_item_type = $this->input->post('advert_item_type', true);
        $advert_title = $this->input->post('advert_title', true);
        $advert_description = $this->input->post('advert_description', true);
        $advert_price_type = $this->input->post('advert_price_type', true);
        $advert_price_value = $this->input->post('advert_price_value', true);
        $advert_price_currency = $this->input->post('advert_price_currency', true);
        $advert_zip_code = $this->input->post('advert_zip_code', true);
        $advert_locality = $this->input->post('advert_locality', true);
        $advert_images = $this->input->post('advert_images', true);
        $advert_year_model = $this->input->post('advert_year_model', true);
        $advert_brand = $this->input->post('advert_brand', true);
        $advert_brand_model = $this->input->post('advert_brand_model', true);
      }

      // Preparing view data
      $view_data = array();
      $view_data['title'] = 'Publish an Advert - Category';
      $view_data['menu_active'] = 'publish_advert';
      $view_data['user_data'] = $this->signed_user;
      $view_data['type'] = $advert_type;
      $view_data['categories'] = $categories;
      $view_data['category_id'] = $category_id;
      $view_data['items'] = $items;
      $view_data['item_types'] = $item_types;
      $view_data['item_extras'] = $item_extras;
      $view_data['brands'] = $brands;
      $view_data['brand_models'] = $brand_models;
      $view_data['give_for'] = $give_for;
      $view_data['peoples'] = $peoples;

      // Loading View
      $this->load_view('advert/publish', $view_data);
      return;
    }

    // Refresh If Something Wrong
    $this->redirect('advert/publish');
  }

  /**
   * Upload API
   * 
   */
  public function upload() {

    // Upload Config
    $config = array();
    $config['upload_path'] = TEMP_IMG_FOLDER;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10240';
    $this->load->library('upload', $config);

    // Upload
    if ($this->upload->do_upload('advert_images_file')) {

      $data = array('upload_data' => $this->upload->data());

      if (!empty($data)) {

        $img_key = $this->loobica_hash(microtime());
        $img_name = $img_key . '.jpg';
        $tmb_name = $img_key . '_thumbnail.jpg';
        $img_file = ADVERT_IMG_FOLDER . $img_name;
        $tmb_file = ADVERT_IMG_FOLDER . $tmb_name;
        $temp_file = $data['upload_data']['full_path'];

        // Generate Cropped Clean JPEG Image
        $this->load->library('loobica/user_image');
        $advert_image = $this->user_image->size(640, 480)->generate($temp_file, $img_file, $tmb_file, false);

        // Updating user
        if ($advert_image) {

          // Delete temp file
          unlink($temp_file);

          // Success - Respond file name
          die(json_encode(array('file' => $img_name)));
        }
      }
    }

    // Error - Respond error
    die(json_encode(array('error' => 'Upload failed. Try again later !')));
  }

}

?>